Fix age histogram to track allocation lifetime instead of age at allo…#40
Merged
Fix age histogram to track allocation lifetime instead of age at allo…#40
Conversation
…cation The age histogram was incorrectly updating at allocation time (when age=0), causing all allocations to go into the 0-1min bucket and never being decremented on free. Changes: - BPF: Remove histogram update from update_age_statistics() - BPF: Remove histogram initialization at first allocation - BPF: Add histogram update in uprobe_free() to track actual lifetime - Rust: Add unfreed allocations to 30+ min bucket (conservative estimate) The histogram now shows: - Buckets 0-2: Count of allocations freed within those lifetimes - Bucket 3 (30+ min): Freed allocations + all unfreed allocations This provides accurate lifetime distribution and highlights long-lived/leaked memory by conservatively counting unfreed allocations as 30+ minutes old. 🤖 Generated with [Claude Code](https://claude.com/claude-code) Co-Authored-By: Claude <noreply@anthropic.com>
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
What's Changed
Overview
This document describes the solution to fix the age histogram feature in Statistics Mode. The solution calculates allocation lifetimes at free time in BPF, and conservatively counts unfreed allocations in userspace.
Problem Summary
The current implementation has two issues:
See issue #39 for detail
Solution Approach
Key Insight
Since Statistics Mode doesn't preserve individual allocation records, we cannot calculate the exact age distribution of currently unfreed allocations. However, we can provide valuable insights by:
This gives us:
Histogram Semantics
After this fix,
age_histogram[4]will represent:The last bucket includes unfreed allocations because:
Implementation Details
Part 1: BPF Changes (malloc_free/bpf/malloc_free.bpf.c)
Change 1: Remove histogram update from allocation time
Location: Lines 284-286 in
update_age_statistics()Current code:
New code:
Change 2: Remove histogram initialization at first allocation
Location: Lines 482-488 in
handle_alloc_return()(new record creation)Current code:
New code:
Change 3: Add histogram update in uprobe_free()
Location: After line 633 in
uprobe_free()(Statistics Mode section)Current code:
New code:
Part 2: Rust Changes (malloc_free/malloc_free.rs)
Change: Add unfreed count to histogram display
Location: Find where age histogram is displayed (if implemented) or prepare for future display
When displaying the age histogram for a process:
Note: The exact location depends on where histogram display is implemented. If not yet implemented, this logic should be added when the feature is exposed to users.
Part 3: Documentation Updates
Update malloc_free.md
Add section explaining age histogram semantics:
Age Distribution:
0-1 min: 1000 allocations (frequent, short-lived - good)
1-5 min: 50 allocations (medium lifetime)
5-30 min: 10 allocations (longer lifetime)
30+ min: 100 allocations (includes 90 unfreed - investigate)
Benefits of This Solution
oldest_ageandavg_ageTesting Plan
After implementation:
Test short-lived allocations:
Test long-lived allocations:
Verify unfreed count:
total_unfreed_countmatches the unfreed allocationsCompare with oldest_age/avg_age:
fixes #39